/**
 * 
 */
package com.ydy.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ydy.annotation.AdminToken;
import com.ydy.annotation.CheckFormRepeat;
import com.ydy.annotation.CtrlParam;
import com.ydy.annotation.StaffToken;
import com.ydy.controller.base.BaseController;
import com.ydy.dto.RelationAdminRoleDTO;
import com.ydy.dto.RelationStaffRoleDTO;
import com.ydy.model.Admin;
import com.ydy.model.Role;
import com.ydy.service.role.RoleService;
import com.ydy.utils.StringUtils;
import com.ydy.vo.other.BaseVo;
import com.ydy.vo.other.PageVo;

/**
 * @author xuzhaojie
 *
 *         2019年1月18日 上午9:17:29
 */
@Controller
@RequestMapping(value = "role", name = "角色模块")
public class RoleController extends BaseController {

	@Autowired
	private RoleService roleService;

	@AdminToken
	@GetMapping(value = "selectByAdmin", name = "管理员查找角色列表")
	@ResponseBody
	public ResponseEntity<PageVo<Role>> selectByAdmin(Role role, Integer page, Integer size) {
		StringUtils.setParamEmptyToNull(role);
		PageVo<Role> vo = roleService.select(role, page, size);
		return ResponseEntity.ok(vo);
	}

	@StaffToken
	@GetMapping(value = "select", name = "店员查找角色列表")
	@ResponseBody
	public ResponseEntity<PageVo<Role>> select(Role role, Integer page, Integer size) {
		StringUtils.setParamEmptyToNull(role);
		role.setShopId(getStaff().getShopId());
		PageVo<Role> vo = roleService.select(role, page, size);
		return ResponseEntity.ok(vo);
	}

	@AdminToken
	@CheckFormRepeat
	@PostMapping(value = "saveByAdmin", name = "管理员保存角色信息")
	@ResponseBody
	public ResponseEntity<Role> saveByAdmin(Role role) {
		Admin admin = getAdmin();
		role.setCreateName(admin.getUsername());
		role.setShopId(0L);
		return ResponseEntity.ok(roleService.saveOrUpdate(role));
	}

	@StaffToken
	@CheckFormRepeat
	@PostMapping(value = "save", name = "店员保存角色信息")
	@ResponseBody
	public ResponseEntity<Role> save(Role role) {
		Admin admin = getAdmin();
		role.setCreateName(admin.getUsername());
		role.setShopId(getStaff().getShopId());
		return ResponseEntity.ok(roleService.saveOrUpdate(role));
	}

	@AdminToken
	@CheckFormRepeat
	@PostMapping(value = "statusByAdmin", name = "管理员角色设置可用不可用")
	@ResponseBody
	public ResponseEntity<BaseVo> statusByAdmin(@CtrlParam("id") Long id) {
		return ResponseEntity.ok(roleService.status(id, null));
	}

	@StaffToken
	@CheckFormRepeat
	@PostMapping(value = "status", name = "店员角色设置可用不可用")
	@ResponseBody
	public ResponseEntity<BaseVo> status(@CtrlParam("id") Long id) {
		return ResponseEntity.ok(roleService.status(id, getStaff()));
	}

	@AdminToken
	@CheckFormRepeat
	@DeleteMapping(value = "deleteByAdmin", name = "管理员刪除角色信息")
	@ResponseBody
	public ResponseEntity<BaseVo> deleteByAdmin(@CtrlParam("id") Long id) {
		return ResponseEntity.ok(roleService.delete(id, null));
	}

	@StaffToken
	@CheckFormRepeat
	@DeleteMapping(value = "delete", name = "店员刪除角色信息")
	@ResponseBody
	public ResponseEntity<BaseVo> delete(@CtrlParam("id") Long id) {
		return ResponseEntity.ok(roleService.delete(id, getStaff()));
	}

	@AdminToken
	@GetMapping(value = "selectByIdByAdmin", name = "管理员查找角色信息")
	@ResponseBody
	public ResponseEntity<Role> selectByIdByAdmin(@CtrlParam("id") Long id) {
		return ResponseEntity.ok(roleService.selectById(id, null));
	}

	@StaffToken
	@GetMapping(value = "selectById", name = "店员查找角色信息")
	@ResponseBody
	public ResponseEntity<Role> selectById(@CtrlParam("id") Long id) {
		return ResponseEntity.ok(roleService.selectById(id, getStaff()));
	}

	@AdminToken
	@GetMapping(value = "selectByAdminId", name = "查找管理员拥有角色列表")
	@ResponseBody
	public ResponseEntity<List<Role>> selectByAdminId(@CtrlParam("adminId") Long adminId) {
		return ResponseEntity.ok(roleService.selectByAdminId(adminId));
	}

	@StaffToken
	@GetMapping(value = "selectByStaffId", name = "查找店员拥有角色列表")
	@ResponseBody
	public ResponseEntity<List<Role>> selectByStaffId(@CtrlParam("staffId") Long staffId) {
		return ResponseEntity.ok(roleService.selectByStaffId(staffId));
	}

	@AdminToken
	@PostMapping(value = "saveAdminRole", name = "保存管理员与角色映射关系")
	@ResponseBody
	public ResponseEntity<BaseVo> saveAdminRoleByAdmin(@RequestBody RelationAdminRoleDTO dto) {
		return ResponseEntity.ok(roleService.saveAdminRole(dto));
	}

	@StaffToken
	@PostMapping(value = "saveStaffRole", name = "保存店员与角色映射关系")
	@ResponseBody
	public ResponseEntity<BaseVo> saveAdminRole(@RequestBody RelationStaffRoleDTO dto) {
		return ResponseEntity.ok(roleService.saveStaffRole(dto));
	}
}